package action;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import bean.Item;
import com.opensymphony.xwork2.ActionSupport;
import dao.CartManager;
import org.apache.struts2.ServletActionContext;

public class ProcessCart extends ActionSupport {
	public String modOrDel(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String type = request.getParameter("action");
		int quantity = Integer.parseInt(request.getParameter("quantity"));
		String goodsId = request.getParameter("goodsId");

		HttpSession session = request.getSession(true);
		ArrayList<Item> cart = (ArrayList<Item>)session.getAttribute("cart");
		CartManager cartManager = new CartManager();

//		System.out.println(goodsId);
		if(type.equalsIgnoreCase("修改")){
			cartManager.setCart(cart);
			cartManager.update(goodsId, quantity);
		}
		if(type.equalsIgnoreCase("删除")){
			cartManager.setCart(cart);
			cartManager.delete(goodsId);
		}

		session.setAttribute("cart", cartManager.getCart());
		return SUCCESS;
	}
}
